package com.beifu.manager.controller;

import com.beifu.manager.dao.OrganizerMapper;
import com.beifu.manager.model.Organizer;
import com.beifu.manager.model.OrganizerExample;
import com.beifu.manager.service.organization.OrganizerService;
import com.mds.manager.model.Organization;
import com.mds.manager.utils.ListUtils;
import com.mds.manager.utils.PageUtils;
import com.mds.manager.utils.ResponseHelp;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static org.apache.shiro.web.filter.mgt.DefaultFilter.user;

/**
 * Created by 再壮 on 2018/6/18.
 */
@Controller
@RequestMapping(value="/sys", produces="text/html;charset=UTF-8")
public class OrganizationController {

    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private OrganizerService organizerService;

    @RequestMapping("/organizerIndex")
    public String goRoleIndex(Model model){
        log.debug(">>>进入角色管理界面");
        return "/system/organization/organizer_index";
    }

    /**
     * 角色列表分页数据
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping("/getOrganizerPage")
    public String getPageData(Model model, HttpServletRequest request, HttpServletResponse res){
        String page = request.getParameter("pageNumber");
        String organizeName = request.getParameter("organizeName");

        OrganizerExample example = new OrganizerExample();
        OrganizerExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(organizeName)) {
            criteria.andOrganizeNameLike("%"+organizeName+"%");
        }

        PageUtils Page = new PageUtils(page);
        Page.setQueryParames(example);
        Page = organizerService.getRolePage(Page);
        return ResponseHelp.responseText(Page);
    }


    @RequestMapping("/goAddOrganization")
    public String goRoleAdd(Model model){
        log.debug("==========进入新增组织页面==========");
        return "/system/organization/organizer_add";
    }

    // 删除
//    delOrganizer   goUpdateOrganizer


    /**
     * 验证组织机构是否存在
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/isExist")
    public String userNameIsExist(Model model,HttpServletRequest request) {
        String organizeName = request.getParameter("organizeName");

        OrganizerExample example = new OrganizerExample();
        OrganizerExample.Criteria criteria = example.createCriteria();
        criteria.andOrganizeNameEqualTo(organizeName);
        List<Organizer> list = organizerService.selectByExample(example);
        if (ListUtils.isNotNull(list)) {
            return ResponseHelp.responseErrorText("机构名称已存在！");
        }
        return ResponseHelp.responseText();
    }


    /**
     * 新增组织
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/organizerAdd")
    public String roleAdd(Model model,HttpServletRequest request) {
        String contacts = request.getParameter("contacts");
        String organizeName = request.getParameter("organizeName");
        String contactsPhone = request.getParameter("contactsPhone");
        String organizeAddress = request.getParameter("organizeAddress");
        String organizeType = request.getParameter("organizeType");

        Organizer organizer = new Organizer();
        organizer.setContacts(contacts);
        organizer.setContactsPhone(contactsPhone);
        organizer.setOrganizeAddress(organizeAddress);
        organizer.setOrganizeName(organizeName);
        organizer.setOrganizeType(Integer.valueOf(organizeType));

        organizerService.insertSelective(organizer);

        return ResponseHelp.responseText();

    }


    @RequestMapping("/goUpdatePage")
    public String goUpdateUserPage(Model model,HttpServletRequest request){
        log.info("进入用户修改界面");

        String id = request.getParameter("id");
        Organizer organization = organizerService.selectByPrimaryKey(Integer.decode(id));

        model.addAttribute("organization", organization);
        return "/system/organization/organizer_update";
    }

    /**
     * 更新用户
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/organizerUpdate")
    public String userUpdate(Model model,HttpServletRequest request){
        String id = request.getParameter("id");
        String contacts = request.getParameter("contacts");
        String organizeName = request.getParameter("organizeName");
        String contactsPhone = request.getParameter("contactsPhone");
        String organizeAddress = request.getParameter("organizeAddress");
        String organizeType = request.getParameter("organizeType");

        Organizer organizer = new Organizer();
        organizer.setId(Integer.valueOf(id));
        organizer.setContacts(contacts);
        organizer.setContactsPhone(contactsPhone);
        organizer.setOrganizeAddress(organizeAddress);
        organizer.setOrganizeName(organizeName);
        organizer.setOrganizeType(Integer.valueOf(organizeType));

        organizerService.updateByPrimaryKey(organizer);

        return ResponseHelp.responseText();
    }


    /**
     * 删除组织
     * @param model
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/delOrganizer")
    public String userDel(Model model,HttpServletRequest request){
        String ids = request.getParameter("ids");
        String[] idsArr = ids.split(",");

        organizerService.batchDel(idsArr);
        return ResponseHelp.responseText();
    }
}
